uninstall.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from __future__ import absolute_import
  2. from pip._vendor.packaging.utils import canonicalize_name
  3. from pip._internal.cli.base_command import Command
  4. from pip._internal.cli.req_command import SessionCommandMixin
  5. from pip._internal.cli.status_codes import SUCCESS
  6. from pip._internal.exceptions import InstallationError
  7. from pip._internal.req import parse_requirements
  8. from pip._internal.req.constructors import (
  9. install_req_from_line,
  10. install_req_from_parsed_requirement,
  11. )
  12. from pip._internal.utils.misc import protect_pip_from_modification_on_windows
  13. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  14. if MYPY_CHECK_RUNNING:
  15. from optparse import Values
  16. from typing import List
  17. class UninstallCommand(Command, SessionCommandMixin):
  18. """
  19. Uninstall packages.
  20. pip is able to uninstall most installed packages. Known exceptions are:
  21. - Pure distutils packages installed with ``python setup.py install``, which
  22. leave behind no metadata to determine what files were installed.
  23. - Script wrappers installed by ``python setup.py develop``.
  24. """
  25. usage = """
  26. %prog [options] <package> ...
  27. %prog [options] -r <requirements file> ..."""
  28. def add_options(self):
  29. # type: () -> None
  30. self.cmd_opts.add_option(
  31. '-r', '--requirement',
  32. dest='requirements',
  33. action='append',
  34. default=[],
  35. metavar='file',
  36. help='Uninstall all the packages listed in the given requirements '
  37. 'file. This option can be used multiple times.',
  38. )
  39. self.cmd_opts.add_option(
  40. '-y', '--yes',
  41. dest='yes',
  42. action='store_true',
  43. help="Don't ask for confirmation of uninstall deletions.")
  44. self.parser.insert_option_group(0, self.cmd_opts)
  45. def run(self, options, args):
  46. # type: (Values, List[str]) -> int
  47. session = self.get_default_session(options)
  48. reqs_to_uninstall = {}
  49. for name in args:
  50. req = install_req_from_line(
  51. name, isolated=options.isolated_mode,
  52. )
  53. if req.name:
  54. reqs_to_uninstall[canonicalize_name(req.name)] = req
  55. for filename in options.requirements:
  56. for parsed_req in parse_requirements(
  57. filename,
  58. options=options,
  59. session=session):
  60. req = install_req_from_parsed_requirement(
  61. parsed_req,
  62. isolated=options.isolated_mode
  63. )
  64. if req.name:
  65. reqs_to_uninstall[canonicalize_name(req.name)] = req
  66. if not reqs_to_uninstall:
  67. raise InstallationError(
  68. 'You must give at least one requirement to {self.name} (see '
  69. '"pip help {self.name}")'.format(**locals())
  70. )
  71. protect_pip_from_modification_on_windows(
  72. modifying_pip="pip" in reqs_to_uninstall
  73. )
  74. for req in reqs_to_uninstall.values():
  75. uninstall_pathset = req.uninstall(
  76. auto_confirm=options.yes, verbose=self.verbosity > 0,
  77. )
  78. if uninstall_pathset:
  79. uninstall_pathset.commit()
  80. return SUCCESS